package edu.zac.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

import java.util.Arrays;

/**
 * @Title
 * @Author Peihan.Zhang
 * @Email zhangpeihan@sjgtw.com
 * @Date 2021/10/22 22:28
 * @Versions 1.0
 */
@Component
@Aspect
public class SpringAop {

    @Pointcut("@annotation(edu.zac.anno.Cache)")
    public void pointcut(){

    }

    @Before("pointcut()")
    public void before(){
        System.out.println("before标签使用了");
    }
    @After("pointcut()")
    public void after(){
        System.out.println("after通知方法执行了");
    }

    @AfterReturning(pointcut = "pointcut()",returning = "result")
    public void afterReturn(JoinPoint joinPoint,Object result){
        System.out.println(joinPoint.getTarget().getClass().getClass());
        System.out.println(Arrays.toString(joinPoint.getArgs()));
        System.out.println(joinPoint.getSignature().getName());
        System.out.println(joinPoint.getSignature().getDeclaringTypeName());
        System.out.println(result);
    }

    @AfterThrowing(pointcut = "pointcut()",throwing = "e")
    public void afterThrowing(JoinPoint joinPoint , Exception e){
        e.printStackTrace();
        System.out.println(e.getMessage());

    }
    @Around("pointcut()")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("around通知方法执行了");
        Object object = null;
        try {
            object =joinPoint.proceed();
        }catch (Exception e){
        }
        System.out.println("around通知方法结束了");
        return  object;

    }

}
